from machine import Pin, ADC
import time

led_pin = Pin(6, Pin.OUT)
ldr_pin = ADC(28)

# 设置初始阈值，根据实际情况调整
threshold = 200

while True:
    # 获取光敏电阻的模拟值
    ldr_value = ldr_pin.read_u16()
    
    # 输出光敏电阻模拟值到控制台
    print("LDR analog value: %s" % ldr_value )
    print(", %s" % (threshold - ldr_value) )
    
    # 如果光线的变化大于20，则点亮LED并延时一段时间
    if threshold - ldr_value > 500:
        
        if threshold == 65535:
            led_pin.on()
            time.sleep(0.1)
            led_pin.off()
        
    # 将新的阈值赋值给 threshold 变量
    threshold = ldr_value

    time.sleep(0.1)